import 'package:lib_base/data_repository/data_repository_interface.dart';
import 'package:lib_base/data_repository/data_repository_manager.dart';
import 'package:rxdart/subjects.dart';

abstract class DataRepository<T> extends DataRepositoryInterface {
  PublishSubject<T?> dataObservable = PublishSubject();

  dynamic encode(T data);

  T? decode(dynamic data);

  @override
  void onDataChanged(dynamic data) {
    T? decodeData = decode(data);
    dataObservable.add(decodeData);
  }

  Future<bool?> setData(T? value) async {
    return DataRepositoryManager.instance
        .setData(id, value != null ? encode(value) : null);
  }

  Future<T?> getData() async {
    dynamic data = await DataRepositoryManager.instance.getData(id);
    return decode(data);
  }
}
